﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.BL.Entity;
using CMS.DAL.Interface;

namespace CMS.DAL.Repository
{
    public interface IImageRepository
    {
        bool DeleteImage(int imageID);
        tbl_Image GetByID(int imageID);
        tbl_Image SaveImage(int categoryID, string desc, int galleryID, short height, bool isCMS, int? order, string path, int siteMapID, string name, short width);
    }

    public class ImageRepository : Repository<tbl_Image>, IImageRepository
    {
        public ImageRepository(IDALContext context) : base(context) { }

        public bool DeleteImage(int imageID)
        {
            tbl_Image image = this.DbSet.FirstOrDefault(i => i.ImageID == imageID);
            if (image == null)
                return false;

            this.Delete(image);
            this.Context.SaveChanges();
            return true;
        }

        public tbl_Image GetByID(int imageID)
        {
            return this.DbSet.FirstOrDefault(i => i.ImageID == imageID);
        }

        public tbl_Image SaveImage(int categoryID, string desc, int galleryID, short height, bool isCMS, int? order, string path, int siteMapID, string name, short width)
        {
            var image = new tbl_Image()
            {
                I_CatID = categoryID,
                I_InCMS = isCMS,
                I_SitemapID = siteMapID,
                I_Gallery = galleryID,
                I_Description = desc,
                I_Height = height,
                I_Order = order,
                I_Path = path,
                I_Width = width
            };
            this.Create(image);
            this.Context.SaveChanges();

            image.I_Thumb = String.Format("{0}{1}.{2}", name.Split('.')[0], image.ImageID, name.Split('.')[1]);

            this.Context.SaveChanges();
            return image;
        }
    }
}
